<template>
  <div class="cate">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-row class="btn">
            <el-col>
                <el-button type="primary" @click="showCateDiologvisble">添加分类</el-button>
            </el-col>
        </el-row>
        <!-- 树形表格 -->
         <tree-table :data="cateList"
         :columns="columns"
         :selection-type="false"
         border
         :expand-type="false"
         :show-index="true">
           <template slot="active" slot-scope="scope">
              <i class="el-icon-error" v-if="scope.row.cat_deleted" style="color:red"></i>
              <i class="el-icon-success " v-else style="color:lightgreen"></i>
           </template>
           <template slot="sort" slot-scope="scope">
              <el-tag v-if="scope.row.cat_level == 0" size="mini">一级</el-tag>
              <el-tag v-else-if="scope.row.cat_level == 1" type="success" size="mini">二级</el-tag>
              <el-tag v-else type="warning" size="mini">三级</el-tag>
           </template>
            <template slot="opt" >
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
               <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
           </template>
         </tree-table>
        <!-- 分页器 -->
          <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="query.pagenum"
                :page-sizes="[3, 5, 7, 10]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
          </el-pagination>
    </el-card>
    <!-- 添加按钮触发的diolog -->
    <el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="50%"
        @close="addDiologBefore"
       >
        <el-form :model="addCateForm" :rules="addCateFormRule" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
            <el-form-item label="分类名称:" prop="cat_name">
              <el-input v-model="addCateForm.cat_name"></el-input>
            </el-form-item>
             <el-form-item label="父级分类:" >
                 <el-cascader
                       expand-trigger="hover"
                      :options="parentList"
                      :props="parentProps"
                      v-model="cascaderKeys"
                      @change="casChange"
                      clearable
                      change-on-select>
                  </el-cascader>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品分类列表
      cateList: [],
      // 获取列表参数
      query: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'active'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 控制diolog
      dialogVisible: false,
      // 表单数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: '',
        cat_level: ''
      },
      // 级联选择器的配置属性
      parentProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联选择器选中的数组
      cascaderKeys: [],
      parentList: [],
      // 添加表单的验证规则对象
      addCateFormRule: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据列表
    async  getCateList () {
      const { data: res } = await this.$axios.get('categories', { params: this.query })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 切换每页数量
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.getCateList()
    },
    // 切换到第几页
    handleCurrentChange (newNum) {
      this.query.pagenum = newNum
      this.getCateList()
    },
    showCateDiologvisble () {
      this.getParentCateList()
      this.dialogVisible = true
    },
    // 获取父级分类
    async  getParentCateList () {
      const { data: res } = await this.$axios.get('categories', { params: { type: 2 } })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类列表失败')
      }
      this.parentList = res.data
    },
    // 级联选择器触发的函数
    casChange () {
      console.log(this.cascaderKeys)

      if (this.cascaderKeys.length > 0) {
        // 获取父级id
        this.addCateForm.cat_pid = this.cascaderKeys[this.cascaderKeys.length - 1]
        // 获取分层层级
        this.addCateForm.cat_level = this.cascaderKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    async  addCate () {
      const { data: res } = await this.$axios.post('categories', this.addCateForm)
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$message.error('添加分类失败')
      }
      this.$message.success('添加分类成功')
      this.getCateList()
      this.dialogVisible = false
    },
    // 添加分类diolog关闭前执行的函数
    addDiologBefore () {
      this.cascaderKeys = []
      this.addCateForm.cat_name = ''
    }
  }
}
</script>

<style scoped>
.btn {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
